package com.tct.worktimeboard.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.config.DataSourceEnum;
import com.tct.worktimeboard.entity.UserInfo;
import com.tct.worktimeboard.log.DataSource;
import com.tct.worktimeboard.mapper.UserInfoMapper;
import com.tct.worktimeboard.mapper.UserMapper;
import com.tct.worktimeboard.mapper.WorkGroupMapper;
import com.tct.worktimeboard.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tct.worktimeboard.util.JsonUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-25
 */
@Slf4j
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    WorkGroupMapper workGroupMapper;

    @DataSource(DataSourceEnum.DB2)
    @Override
    public JSONObject getHavePowerByWorkTime(Long userId) {
        List<LinkedHashMap<String, Object>> haveWorkTimePower = userInfoMapper.getHaveWorkTimePower();
        List<LinkedHashMap<String, Object>> collect = haveWorkTimePower.stream().distinct().collect(Collectors.toList());
        return JsonUtil.toJsonData(collect);
    }

    @DataSource(DataSourceEnum.DB1)
    @Transactional
    public List<Long> getHavePowerByGroup(Long userId) {
        List<Long> groupIdByUserId = workGroupMapper.getGroupIdByUserId(userId);
        log.info("该用户所在群组ID->" + groupIdByUserId);
        return workGroupMapper.getPowerUserByGroupList(groupIdByUserId);
    }

    @DataSource(DataSourceEnum.DB2)
    @Override
    public JSONObject getInitPowerUserList(Long userId) {
        return JsonUtil.toJsonData(userInfoMapper.getInitPowerUserList(userId));
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户id
     * @return UserInfo object
     */
    @DataSource(DataSourceEnum.DB2)
    @Override
    public JSONObject getUserInfo(Long userId) {
        return JsonUtil.toJsonData(userInfoMapper.getUserInfo(userId));
    }

    @DataSource(DataSourceEnum.DB2)
    @Transactional
    @Override
    public JSONObject refreshDept(List<String> deptList) {
        userInfoMapper.deleteDept();
        ;
        userInfoMapper.insertDept(deptList);
        return JsonUtil.toJsonData();
    }

    @DataSource(DataSourceEnum.DB2)
    @Override
    public List<Long> getUserIdByName(List<String> nameList) {
        List<UserInfo> userInfos = userInfoMapper.selectList(new QueryWrapper<UserInfo>().select("id").in("name", nameList));
        List<Long> userIdList = new ArrayList<>();
        for (UserInfo info: userInfos) {
            userIdList.add(info.getId());
        }
        return userIdList;
    }

    @DataSource(DataSourceEnum.DB2)
    @Override
    public JSONObject getUserIdByAccount(String account) {
        return JsonUtil.toJsonData(userInfoMapper.getUserIdByAccount(account));
    }

    /**
     * 添加权限
     * @param powerName 权限名称
     * @param nameList 用户列表
     * @return
     */
    @DataSource(DataSourceEnum.DB2)
    @Override
    public int addPowerByUserId(Long powerName, List<Long> nameList) {
        return userInfoMapper.addPowerByUserList(powerName,nameList);
    }
}
